package ninja.awesome.game.gameobject;

import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Image;
import ninja.awesome.framework.Sound;
import ninja.awesome.game.MainGameScreen;
import ninja.awesome.game.NinjaGame;
import ninja.awesome.game.classes.Friendly;
import ninja.awesome.game.compat.Rectangle;
import ninja.awesome.game.main.Animation;
import ninja.awesome.game.main.Controller;
import ninja.awesome.game.main.Sprites;

public class Attack extends GameObj implements Friendly {

	private String direction;
	private int speed = 16; // I put 0 since the swing shall not move
	private int timer = 0, combo = -1;
	private int swingTimer;
	private boolean hit = false;

	private Image[] rightSwings;
	private Image[] leftSwings;

	Image image;
	Image[] e;
	Image[] weapon;
	MainGameScreen game;
	Sound boom;
	Animation explosion;
	Controller c;
	int flag = 0; // to indicate which attacck we're using 0 for normal, 1 for
					// shuriken

	Animation currentAttack; // to show attack for player!!!

	private int selectPicture = 0;
	private Sprites s;

	public Attack(int x, int y, String direction, MainGameScreen game, Controller c, Sprites s, int combo, int flag) {
		super(x, y);
		this.game = game;
		this.c = c;
		this.direction = direction;
		this.s = s;
		this.swingTimer = 0;
		this.combo = combo;
		this.flag = flag;
		if (flag == 0) {
			rightSwings = this.s.getAttackSprite();
			currentAttack = new Animation(5, rightSwings[0], 250, 250, 4, combo);
			e = s.getExplosion();

			leftSwings = this.s.getAttackSprite();
			currentAttack = new Animation(5, leftSwings[1], 250, 250, 4, combo);
			e = s.getExplosion();
		} else if (flag == 1) {
			e = this.s.getShuriken();
			currentAttack = new Animation(5, e[0], 15, 15, 8);
		}

		explosion = new Animation(5, image, 32, 32, 1);
		// boom = new Sound("/resources/snd_explosion1.wav");
	}

	@Override
	public void update() {
		if (flag == 0) {
			swingTimer++;
			if (swingTimer >= 4) {
				c.removeFriendly(this);
			}
			if (!hit) {
				if (timer == 40) {
					c.removeFriendly(this);
					timer++;

				}
			} else if (hit) {
				if (timer >= 40) {
					c.removeFriendly(this);
				}
				timer++;
				// explosion.runAnimation();
			}
		} else if (flag == 1) {
			if (!hit) {
				if (direction == "right") {
					xLoc += speed;
					// System.out.println(xLoc);
				}
				if (direction == "left") {
					xLoc -= speed;
					// System.out.println(xLoc);
				}
				// PHU: update position related to speed of ninja
				xLoc -= game.getNinja().getSpeedX();
			} else if (hit) {
				c.removeFriendly(this);
			}
			if (xLoc < -20 || xLoc > NinjaGame.WIDTH + 20) {
				c.removeFriendly(this);
			}
		}

	}

	@Override
	public void draw(Graphics g) {
		if (flag == 0) {
			if (game.getNinja().getDirection().equals("right")) {
				currentAttack.setImage(rightSwings[0]);
				currentAttack.runAnimation();
				currentAttack.drawAnimation(g, xLoc, yLoc);
			} else if (game.getNinja().getDirection().equals("left")) {
				currentAttack.setImage(leftSwings[1]);
				currentAttack.runAnimation();
				currentAttack.drawAnimation(g, xLoc, yLoc);
			}
		} else if (flag == 1) {
			if (!hit) {
				currentAttack.runAnimation();
				currentAttack.drawAnimation(g, xLoc, yLoc);
			}

		}
	}

	@Override
	public Rectangle getBounds() {
		Rectangle a = null;
		if (flag == 0) {
			a = new Rectangle(xLoc, yLoc, 200, 200);
		} else if (flag == 1) {
			a = new Rectangle(xLoc, yLoc, 15, 15);
		}
		return a;
	}

	// GETTERS
	@Override
	public int getX() {
		return xLoc;
	}

	@Override
	public int getY() {
		return yLoc;
	}

	// Setters
	public void SetX(int x) {
		this.xLoc = x;
	}

	public void SetY(int y) {
		this.yLoc = y;
	}

	public int getTimer() {
		return timer;
	}

	public void setTimer(int timer) {
		this.timer = timer;
	}

	public boolean getHit() {
		return hit;
	}

	public void setHit(boolean hit) {
		this.hit = hit;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public String getDirection() {
		return this.direction;
	}

	public void setSelectPicture() { // assign the picture
		this.selectPicture = selectPicture;
	}

	public int getSelectPicture() { // get the selected picture
		return this.selectPicture;
	}

}
